<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区" prop="areaName">
        <el-select v-model="queryParams.areaName":filter-method="filterMethod" placeholder="请输入小区" filterable allow-create
                   clearable size="small"  default-first-option>
          <el-option v-for="(item, index) in areaIdOptions" :key="index" :label="item.areaName"
                     :value="item.areaName" :disabled="item.disabled" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="缴费日期">
        <el-date-picker
          v-model="dateRange"
          end-placeholder="结束日期"
          range-separator="-"
          size="small"
          start-placeholder="开始日期"
          style="width: 240px"
          type="daterange"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          icon="el-icon-download"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="moneyFlowList" >
      <el-table-column label="小区" align="center" prop="areaName" width="120"/>
      <el-table-column :formatter="numberFormat" label="总面积" align="center" prop="spaceSum" width="120"/>
      <el-table-column  :formatter="numberFormat"  label="收入户数" align="center" prop="shouSum" width="100"/>
      <el-table-column  :formatter="numberFormat"  label="收入金额" align="center" prop="shouMoney" width="120"/>
      <el-table-column  :formatter="numberFormat"  label="退款户数" align="center" prop="tuiSum" width="100"/>
      <el-table-column  :formatter="numberFormat"  label="退款金额" align="center" prop="tuiMoney" width="120"/>
      <el-table-column  :formatter="numberFormat"  label="返还户数" align="center" prop="fanSum" width="100"/>
      <el-table-column  :formatter="numberFormat"  label="返还金额" align="center" prop="fanMoney" width="120"/>
      <el-table-column  :formatter="numberFormat"  label="维修户数" align="center" prop="paySum" width="100"/>
      <el-table-column  :formatter="numberFormat"  label="拨付金额" align="center" prop="payMoney" width="120"/>
      <el-table-column  :formatter="numberFormat"  label="利息金额" align="center" prop="rateMoney" width="120"/>

      <el-table-column label="操作" align="center"  fixed="right"  class-name="small-padding fixed-width"width="120" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="costQuery(scope.row)"
            :style="{ display: scope.row.areaName=='合计'?'none':'' }"
          >缴费明细</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="repairQuery(scope.row)"
            :style="{ display: scope.row.areaName=='合计'?'none':'' }"
          >分摊明细</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="rateQuery(scope.row)"
            :style="{ display: scope.row.areaName=='合计'?'none':'' }"
          >利息明细</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <ul v-show="visible" :style="{left:left+'px',top:top+'px'}" class="contextmenu">
      <li @click="costQuery"> 缴费明细 </li>
      <li @click="repairQuery"> 分摊明细 </li>
      <li @click="rateQuery"> 利息明细 </li>
    </ul>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看缴费记录 -->
    <el-dialog :title="title" :visible.sync="openCost" width="1000px" append-to-body>
      <el-form :model="queryParams1" :inline="true" v-show="showSearch" label-width="68px">
        <el-table v-loading="loading" :data="costQueryList">
          <el-table-column label="地址" align="center" prop="doorAddr" width="200"/>
          <el-table-column label="房屋编号" align="center" prop="houseId" />
          <el-table-column  :formatter="numberFormat"  label="缴费金额" align="center" prop="costMoney" />
          <el-table-column label="缴费时间" align="center" prop="costDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.costDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" align="userName" prop="userName" />
        </el-table>
        <pagination
          v-show="total>0"
          :total="total1"
          :page.sync="queryParams1.pageNum"
          :limit.sync="queryParams1.pageSize"
          @pagination="getCostList"
        />
      </el-form>
    </el-dialog>

    <!-- 查看计息明细记录 -->
    <el-dialog :title="title" :visible.sync="openRate" width="1000px" append-to-body>
      <el-form :model="queryParams2" :inline="true" v-show="showSearch" label-width="68px">
        <el-table v-loading="loading" :data="rateQueryList">
          <el-table-column label="地址" align="center" prop="doorAddr" width="200"/>
          <el-table-column label="房屋编号" align="center" prop="houseId" />
          <el-table-column  :formatter="numberFormat"  label="利息合计" align="center" prop="rateMoney"/>
          <el-table-column label="计息日" align="center" prop="rateDate">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.rateDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total2"
          :page.sync="queryParams2.pageNum"
          :limit.sync="queryParams2.pageSize"
          @pagination="getRateList"
        />
      </el-form>
    </el-dialog>

    <!-- 查看缴费明细记录 -->
    <el-dialog :title="title" :visible.sync="openRepair" width="1000px" append-to-body>
      <el-form :model="queryParams3" :inline="true" v-show="showSearch" label-width="68px">
        <el-table v-loading="loading" :data="repairQueryList">
          <el-table-column label="工程编号" align="center" prop="projectId" />
          <el-table-column label="地址" align="center" prop="doorAddr" width="200"/>
          <el-table-column label="房屋编号" align="center" prop="houseId" />
          <el-table-column label="姓名" align="userName" prop="userName" />
          <el-table-column  :formatter="numberFormat"  label="当前余额" align="center" prop="balance" />
          <el-table-column  :formatter="numberFormat"  label="拨付金额" align="center" prop="payMoney" />
          <el-table-column  :formatter="numberFormat"  label="不足金额" align="center" prop="noMoney" />
          <el-table-column label="分摊日期" align="center" prop="executeDate">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.executeDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total3"
          :page.sync="queryParams3.pageNum"
          :limit.sync="queryParams3.pageSize"
          @pagination="getRepairList"
        />
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { listMoneyFlow,getAreaInfo,getAreaInfo1,listCostQuery,listRateQuery,listRepairQuery} from "@/api/report/moneyFlow";

export default {
  name: "MoneyFlow",
  data() {
    return {
      visible: false,
      left: 0,
      top: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 日期范围
      dateRange: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total1: 0,
      total2: 0,
      total3: 0,
      // 缴费信息表格数据
      moneyFlowList: [],
      costQueryList: [],
      rateQueryList: [],
      repairQueryList: [],
      areaIdOptions:[{}],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openCost: false,
      openRate: false,
      openRepair: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
        areaName:null,
        costDate: null,
        costSts: null,
        costWay: null,
        checkSts: null,
        billNo: null,
      },
      // 查询缴费参数
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
        areaName: null
      },
      // 查询计息参数
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
        rateMoney: null,
        areaName: null,
        rateDate: null
      },
      // 查询分摊参数
      queryParams3: {
        pageNum: 1,
        pageSize: 10,
        Name: null,
        groupName: null,
        unitName: null,
        doorName: null,
        areaName: null,
        houseId: null,
        projectId: null,
        executeDate: null,
        payMoney: null,
        noMoney: null,
        balance: null,
        startDate:null,
        endDate:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.filterMethod();
    this.areaSelect();
  },
  methods: {
    numberFormat (row, column, cellValue) {
      cellValue += ''
      if (!cellValue.includes('.')) cellValue += '.'
      return cellValue.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
        return $1 + ','
      }).replace(/\.$/, '')
    },
    filterMethod(val){
      this.value=val
      getAreaInfo1(this.value).then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    areaSelect(){
      getAreaInfo().then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    /** 查询缴费信息列表 */
    getList() {
      this.loading = true;
        listMoneyFlow(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.moneyFlowList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.areaSelect()
      });
    },
    /** 查询缴费信息列表 */
    getCostList() {
      debugger
      this.loading = true;
        listCostQuery(this.addDateRange(this.queryParams1, this.dateRange)).then(response => {
        this.costQueryList = response.rows;
        this.total1 = response.total;
        this.loading = false;
      });
    },
    /** 查询计息明细列表 */
    getRateList() {
      this.loading = true;
      listRateQuery(this.addDateRange(this.queryParams2, this.dateRange)).then(response => {
        this.rateQueryList = response.rows;
        this.total2 = response.total;
        this.loading = false;
      });
    },
    /** 查询计息明细列表 */
    getRepairList() {
      this.loading = true;
      listRepairQuery(this.addDateRange(this.queryParams3, this.dateRange)).then(response => {
        this.repairQueryList = response.rows;
        this.total3 = response.total;
        this.loading = false;
      });
    },
    costQuery(row) {
      const areaName = row.areaName;
      this.queryParams1.areaName=areaName
      this.openCost = true;
      this.getCostList()
    },
    repairQuery(row) {
      const areaName = row.areaName;
      this.queryParams3.areaName=areaName
      this.openRepair = true;
      this.getRepairList()
    },
    /** 获取计息记录 */
    rateQuery(row) {
      const areaName = row.areaName;
      this.queryParams2.areaName=areaName
      this.openRate = true;
      this.getRateList()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.areaSelect();
      this.dateRange = [];
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/report/moneyFlow/export', {
        ...this.queryParams
      }, `收支查询_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>


<style lang="scss" scoped>

.contextmenu {
  margin: 0;
  background: #fff;
  z-index: 3000;
  position: absolute;
  list-style-type: none;
  padding: 5px 0;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 400;
  color: #333;
  box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .3);
  li {
    margin: 0;
    padding: 7px 16px;
    cursor: pointer;
    &:hover {
      background: #eee;
    }
  }
}

</style>
